package com.xtkj.superclub_information.controller;

import com.xtkj.commons.JsonParse;
import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.commons.TokenUtil;
import com.xtkj.superclub_entity.entity.travel.Travel;
import com.xtkj.superclub_entity.entity.travel.TravelComment;
import com.xtkj.superclub_entity.entity.travel.TravelCommentReply;
import com.xtkj.superclub_entity.entity.travel.TravelPraise;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_information.client.IUserClient;
import com.xtkj.superclub_information.dao.TravelDao;
import com.xtkj.superclub_information.service.ITravelCommentService;
import com.xtkj.superclub_information.service.ITravelPraiseService;
import com.xtkj.superclub_information.service.ITravelReplyService;
import com.xtkj.superclub_information.service.ITravelService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lizheqi
 * @create 2020-01-02-10:15
 */
@RestController
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelReplyService travelReplyService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private IUserClient userClient;
    @Autowired
    private TravelDao travelDao;

    //消息队列
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate1;
    /**
     * 根据最新评论获取所有的游记列表
     * @return
     */
    @RequestMapping(value = "/listByNews",method = RequestMethod.GET)
    public JsonResult getTravelsListByNews(){
        try{
            List<Travel> travels = travelService.getTravelsListByNews();
            return JsonResult.success().setMsg("获取游记列表成功").setData(travels).setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("获取游记列表失败").setCode(400);
        }
    }

    /**
     * 根据创建日期获取所有的游记列表
     * @return
     */
    @RequestMapping(value = "/listByTime",method = RequestMethod.GET)
    public JsonResult getTravelsListByTime(){
        try{
            List<Travel> travels = travelService.getTravelsListByTime();
            return JsonResult.success().setMsg("获取游记列表成功").setData(travels).setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("获取游记列表失败").setCode(400);
        }
    }

    /**
     * 获取热门游记列表
     * @return
     */
    @RequestMapping(value = "/listByHot",method = RequestMethod.GET)
    public JsonResult getTravelsListByHot(){
        try{
            List<Travel> travels = travelService.getTravelsListByHot();
            return JsonResult.success().setMsg("获取游记列表成功").setData(travels).setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("获取游记列表失败").setCode(400);
        }
    }

    /**
     * 获取游记详情
     * @return
     */
    @RequestMapping(value = "/getInfo",method=RequestMethod.POST)
    public JsonResult getTravelsInfo(@RequestBody Travel travel,HttpServletRequest request){
        try{
            String userId = TokenUtil.getUserId(request.getHeader("token"));
            Map map = travelService.getTravelsInfo(travel,userId);
            return JsonResult.success().setMsg("获取游记详情成功").setData(map).setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("获取游记详情失败").setCode(400);
        }
    }

    /**
     * 上传照片
     * @param file
     * @return
     */
    @RequestMapping(value="/uploadPhoto",method = RequestMethod.POST)
    public JsonResult uploadPhoto(@RequestBody MultipartFile file){
        try{
            //传回照片地址
            String address = travelService.uploadPhoto(file);
            return JsonResult.success().setMsg("上传照片成功").setCode(200).setData(address);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("上传照片失败").setCode(400);
        }
    }


    /**
     * 游记投稿接口
     * @param travel
     * @return
     */
    @RequestMapping(value="/contribute",method = RequestMethod.POST)
    public JsonResult travelsContribute(@RequestBody Travel travel){
        try{
            travelService.travelsContribute(travel);
            return JsonResult.success().setMsg("游记投稿成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("游记投稿失败").setCode(400);
        }
    }

    /**
     * 游记点赞功能
     * @return
     */
    @RequestMapping(value = "/addPraise",method = RequestMethod.POST)
    public JsonResult addPraise(@RequestBody TravelPraise travelPraise){
        try{
            //通过拿到用户ID查找用户点赞人
            JsonResult jsonResult = userClient.getUser(new User().setUserId(travelPraise.getUserId()));
            User praiseUser = JsonParse.string2Obj(jsonResult.getData().toString(), User.class);
            //通过游记编号查询游记信息
            Travel travel = travelDao.selectById(travelPraise.getTravelId());
            //从而找到游记发布人的信息
            JsonResult json = userClient.getUser(new User().setUserId(travel.getUserId()));
            User travelUser = JsonParse.string2Obj(json.getData().toString(), User.class);
            String message = praiseUser.getNickName()+"给"+travelUser.getNickName()+"的"+travel.getTravelTitle()+"的游记点赞了";
            //使用游记编号与点赞用户编号作为字符串传递
            String info = travelPraise.getTravelId()+"-"+travelPraise.getUserId()+"-"+message;
            rabbitTemplate1.convertAndSend("praiseQueue",info);
            return JsonResult.success().setMsg("游记点赞成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("游记点赞失败").setCode(400);
        }
    }

    /**
     * 游记评论回复
     * @return
     */
    @RequestMapping(value = "/reply",method = RequestMethod.POST)
    public JsonResult reply(@RequestBody TravelCommentReply travelCommentReply){
        try{
            String travelCommentReplyId = snowflakeIdWorker.nextId()+"";
            travelCommentReply.setTravelCommentReplyId(travelCommentReplyId);
            travelReplyService.reply(travelCommentReply);
            return JsonResult.success().setMsg("游记评论回复成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("游记评论回复失败").setCode(400);
        }
    }

    /**
     * 游记评论
     * @param travelComment
     * @return
     */
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    public JsonResult comment(@RequestBody TravelComment travelComment){
        try{
            String travelCommentId = snowflakeIdWorker.nextId()+"";
            travelComment.setTravelCommentId(travelCommentId);
            travelCommentService.comment(travelComment);
            return JsonResult.success().setMsg("游记评论成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setMsg("游记评论失败").setCode(400);
        }
    }

}
